package q965_isUnivalTree;

import CommonClass.Common.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution_1 {
    /*
    BFS
     */
    public boolean isUnivalTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int x = root.val;
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.val != x) return false;
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
        }

        return true;
    }
}
